﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DbSharp.Utils.IO;

namespace DbSharp.Framework.Storage
{
    public static class StorageNameMaker
    {
        private static void PrepareFileName(string fileNameFmt, out string fileFullName)
        {
            //must use the local user work path, or it will cause access denied error On vista system
            string basePath = PathHelper.GetLocalAppDataPath();

            string outputPath = Path.Combine(basePath, "output");
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }
            while (true)
            {
                fileFullName = string.Format(fileNameFmt,Guid.NewGuid().ToString("N"));
                fileFullName = Path.Combine(outputPath, fileFullName);

                if (File.Exists(fileFullName))
                    continue;
                else
                    break;
            }
        }

        public static void PrepareExportXmlFileName(out string fileFullName)
        {
            string dataFileNameFmt = "xml.{0}.xm";
            PrepareFileName(dataFileNameFmt, out  fileFullName);
        }

        public static void PrepareExcelDiffFileName(string filePrefix, out string fileFullName)
        {
            if (string.IsNullOrEmpty(filePrefix))
                filePrefix = "data";
            string fileNameFmt = filePrefix + ".{0}.xls";
            PrepareFileName(fileNameFmt, out fileFullName);
        }

        public static void PrepareExcel2007DiffFileName(string filePrefix, out string fileFullName)
        {
            if (string.IsNullOrEmpty(filePrefix))
                filePrefix = "data";
            string fileNameFmt = filePrefix + ".{0}.xlsx";
            PrepareFileName(fileNameFmt, out fileFullName); 
        }

        public static void PrepareHtmlDiffFileName(string filePrefix, out string fileFullName)
        {
            if (string.IsNullOrEmpty(filePrefix))
                filePrefix = "data";
            string fileNameFmt = filePrefix + ".{0}.htm";
            PrepareFileName(fileNameFmt, out fileFullName);
        }

        public static void PrepareTableName(out string tableName)
        {
            string tableNameFmt = "wk{0}";
            Guid guid = Guid.NewGuid();
            tableName = string.Format(tableNameFmt, guid.ToString("N"));
        }
    }
}
